/*******************************************************************************
 * Copyright 2020 huanggefan.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

const tools = require("../../js/tools");

window.onload = function () {
    tools.LoadToken();
    if (document.getElementById("manager-index-page") !== null) {
        selectManagerMenuNavBar();
        listenLogoutClick();
        tools.CheckTokenExist();
        tools.ConfirmLeavePage();
    }
    if (document.getElementById("manager-college-page") !== null) {
        selectManagerCollegeNavBar();
        collegeManage_CollegeSelectBind();
        createCollege();
        updateCollege();
        setCollegeManager();
        unsetCollegeManager();
        deleteCollege();
    }
    if (document.getElementById("manager-major-page") !== null) {
        selectManagerMajorNavBar();
        majorManage_CollegeSelectBind();
        majorManage_MajorSelectBind();
        createMajor();
        updateMajor();
        setMajorManager();
        unsetMajorManager();
        deleteMajor();
    }
    if (document.getElementById("manager-class-page") !== null) {
        selectManagerClassNavBar();
        classManage_CollegeSelectBind();
        classManage_MajorSelectBind();
        classManage_ClassSelectBind();
        createClass();
        updateClass();
        setClassManager();
        unsetClassManager();
        deleteClass();
    }
    if (document.getElementById("manager-course-page") !== null) {
        selectManagerCourseNavBar();
        courseManage_CollegeSelectBind();
        courseManage_MajorSelectBind();
        courseManage_CourseSelectBind();
        createCourse();
        updateCourse();
        setCourseManager();
        unsetCourseManager();
        deleteCourse();
        selectCourse();
        createChapter();
        setChapterManager();
        unsetChapterManager();
        deleteChapter();
        selectChapter();
        saveChapterContent();
        rollBackChapterContent();
        displayChapterContent();
    }
    if (document.getElementById("manager-question-page") !== null) {
        selectManagerQuestionNavBar();
        questionManage_CollegeSelectBind();
        questionManage_MajorSelectBind();
        questionManage_CourseSelectBind();
        questionBank_selectCourse();
        questionBank_createBank();
        questionBank_update();
        questionBank_setBankManager();
        questionBank_unsetBankManager();
        questionBank_deleteBank();
        questionBank_selectBank();
        deleteQuestion();
        createSingleQuestion();
        createMultipleQuestion();
        createJudgeQuestion();
    }
    if (document.getElementById("manager-user-page") !== null) {
        selectManagerUserNavBar();
    }
};

function selectManagerMenuNavBar() {
    let navURL = {
        "navCollege": "manager-college.html",
        "navMajor": "manager-major.html",
        "navClass": "manager-class.html",
        "navCourse": "manager-course.html",
        "navQuestion": "manager-question.html",
        "navUser": "manager-user.html",
        "navSetting": "setting.html"
    };

    let navLinkList = [
        "navCollege",
        "navMajor",
        "navClass",
        "navCourse",
        "navQuestion",
        "navUser",
        "navSetting"
    ];

    let iframeElm = document.getElementById("managerIframeContainer");
    if (iframeElm === null) {
        return;
    }
    if (iframeElm.src !== "manager-college.html") {
        iframeElm.src = "manager-college.html";
    }

    function doListenClick(id) {
        for (let i = 0; i < navLinkList.length; i++) {
            let e = document.getElementById(navLinkList[i]);
            if (e === null) {
                return
            }
            e.classList.remove("bg-info");
            if (navLinkList[i] !== "navSetting") {
                e.classList.add("bg-primary");
            } else {
                e.classList.add("bg-secondary");
            }
        }

        let e = document.getElementById(id);
        if (e === null) {
            return
        }
        if (id !== "navSetting") {
            e.classList.remove("bg-primary");
        } else {
            e.classList.remove("bg-secondary");
        }
        e.classList.add("bg-info");
        iframeElm.src = navURL[id];
    }

    for (let i = 0; i < navLinkList.length; i++) {
        let id = navLinkList[i];
        let e = document.getElementById(id);
        if (e === null) {
            return
        }
        e.onclick = function () {
            doListenClick(id)
        };
    }
}

function listenLogoutClick() {
    let e = document.getElementById("navLogout");
    if (e === null) {
        return
    }
    e.onclick = function () {
        tools.RemoveCookie("AuthToken");
        window.location.href = "/index.html";
    }
}

// nav bar

function selectManagerUserNavBar() {
    let elIDs = [
        "user-create",
        "user-role",
        "user-reset",
        "user-delete",
        "user-load"
    ];
    let formIDs = [
        "create",
        "role",
        "reset",
        "delete",
        "load"
    ];
    let elIDForm = {
        "user-create": "create",
        "user-role": "role",
        "user-reset": "reset",
        "user-delete": "delete",
        "user-load": "load"
    };
    letNavBindForm(elIDs, formIDs, elIDForm);
}

function selectManagerCollegeNavBar() {
    let elIDs = [
        "college-create",
        "college-update",
        "college-set",
        "college-unset",
        "college-delete",
        "college-load"
    ];
    let formIDs = [
        "create",
        "update",
        "set",
        "unset",
        "delete",
        "load"
    ];
    let elIDForm = {
        "college-create": "create",
        "college-update": "update",
        "college-set": "set",
        "college-unset": "unset",
        "college-delete": "delete",
        "college-load": "load"
    };
    letNavBindForm(elIDs, formIDs, elIDForm);
}

function selectManagerMajorNavBar() {
    let elIDs = [
        "major-create",
        "major-update",
        "major-set",
        "major-unset",
        "major-delete",
        "major-load"
    ];
    let formIDs = [
        "create",
        "update",
        "set",
        "unset",
        "delete",
        "load"
    ];
    let elIDForm = {
        "major-create": "create",
        "major-update": "update",
        "major-set": "set",
        "major-unset": "unset",
        "major-delete": "delete",
        "major-load": "load"
    };
    letNavBindForm(elIDs, formIDs, elIDForm);
}

function selectManagerClassNavBar() {
    let elIDs = [
        "class-create",
        "class-update",
        "class-set",
        "class-unset",
        "class-delete",
        "class-load"
    ];
    let formIDs = [
        "create",
        "update",
        "set",
        "unset",
        "delete",
        "load"
    ];
    let elIDForm = {
        "class-create": "create",
        "class-update": "update",
        "class-set": "set",
        "class-unset": "unset",
        "class-delete": "delete",
        "class-load": "load"
    };
    letNavBindForm(elIDs, formIDs, elIDForm);
}

function selectManagerCourseNavBar() {
    let elIDs = [
        "course-create",
        "course-update",
        "course-set",
        "course-unset",
        "course-delete",
        "course-choose",
        "course-chapter",
        "course-chapterEdit"
    ];
    let formIDs = [
        "create",
        "update",
        "set",
        "unset",
        "delete",
        "choose",
        "chapter",
        "chapterEdit"
    ];
    let elIDForm = {
        "course-create": "create",
        "course-update": "update",
        "course-set": "set",
        "course-unset": "unset",
        "course-delete": "delete",
        "course-choose": "choose",
        "course-chapter": "chapter",
        "course-chapterEdit": "chapterEdit"
    };
    letNavBindForm(elIDs, formIDs, elIDForm);
}

function selectManagerQuestionNavBar() {
    let elIDs = [
        "question-choose",
        "question-createB",
        "question-updateB",
        "question-setB",
        "question-unsetB",
        "question-deleteB",
        "question-chooseB",
        "question-editQ"
    ];
    let formIDs = [
        "choose",
        "create",
        "update",
        "set",
        "unset",
        "delete",
        "chooseB",
        "bank"
    ];
    let elIDForm = {
        "question-choose": "choose",
        "question-createB": "create",
        "question-updateB": "update",
        "question-setB": "set",
        "question-unsetB": "unset",
        "question-deleteB": "delete",
        "question-chooseB": "chooseB",
        "question-editQ": "bank"
    };
    letNavBindForm(elIDs, formIDs, elIDForm);
}

function letNavBindForm(navIDs, formIDs, nav2Form) {
    let changeNavBarColor = function (id) {
        for (let i = 0; i < navIDs.length; i++) {
            let e = document.getElementById(navIDs[i]);
            if (e === null) {
                return
            }
            e.className = "text-secondary";
        }
        let l = document.getElementById(id);
        if (l === null) {
            return;
        }
        l.className = "text-primary"
    };
    let changeFormDisplay = function (id) {
        for (let i = 0; i < formIDs.length; i++) {
            let e = document.getElementById(formIDs[i]);
            if (e === null) {
                return
            }
            e.style.display = "none";
        }
        let l = document.getElementById(id);
        if (l === null) {
            return;
        }
        l.style.display = "flex";
    };

    for (let i = 0; i < navIDs.length; i++) {
        let e = document.getElementById(navIDs[i]);
        if (e === null) {
            return
        }
        e.onclick = function () {
            let id = navIDs[i];
            changeNavBarColor(id);
            changeFormDisplay(nav2Form[id]);
        }
    }
}

// select bind

function collegeManage_CollegeSelectBind() {
    let selectChild = collegeSelectBind_InnerHtml();

    document.getElementById("update-college").innerHTML = selectChild;
    document.getElementById("set-college").innerHTML = selectChild;
    document.getElementById("unset-college").innerHTML = selectChild;
    document.getElementById("delete-college").innerHTML = selectChild;
    document.getElementById("delete-verifyCollege").innerHTML = selectChild;
}

function majorManage_CollegeSelectBind() {
    let selectChild = collegeSelectBind_InnerHtml();

    document.getElementById("create-college").innerHTML = selectChild;
    document.getElementById("update-college").innerHTML = selectChild;
    document.getElementById("set-college").innerHTML = selectChild;
    document.getElementById("unset-college").innerHTML = selectChild;
    document.getElementById("delete-college").innerHTML = selectChild;
}

function majorManage_MajorSelectBind() {
    let collegeUUID = document.getElementById("update-college").value;
    document.getElementById("update-major").innerHTML = majorSelectBind_InnerHtml(collegeUUID);
    document.getElementById("update-college").onblur = function () {
        let collegeUUID = document.getElementById("update-college").value;
        document.getElementById("update-major").innerHTML = majorSelectBind_InnerHtml(collegeUUID);
    };

    collegeUUID = document.getElementById("set-college").value;
    document.getElementById("set-major").innerHTML = majorSelectBind_InnerHtml(collegeUUID);
    document.getElementById("set-college").onblur = function () {
        let collegeUUID = document.getElementById("set-college").value;
        document.getElementById("set-major").innerHTML = majorSelectBind_InnerHtml(collegeUUID);
    };

    collegeUUID = document.getElementById("unset-college").value;
    document.getElementById("unset-major").innerHTML = majorSelectBind_InnerHtml(collegeUUID);
    document.getElementById("unset-college").onblur = function () {
        let collegeUUID = document.getElementById("unset-college").value;
        document.getElementById("unset-major").innerHTML = majorSelectBind_InnerHtml(collegeUUID);
    };

    collegeUUID = document.getElementById("delete-college").value;
    let i = majorSelectBind_InnerHtml(collegeUUID);
    document.getElementById("delete-major").innerHTML = i;
    document.getElementById("delete-verifyMajor").innerHTML = i;
    document.getElementById("delete-college").onblur = function () {
        let collegeUUID = document.getElementById("delete-college").value;
        let i = majorSelectBind_InnerHtml(collegeUUID);
        document.getElementById("delete-major").innerHTML = i;
        document.getElementById("delete-verifyMajor").innerHTML = i;
    };
}

function classManage_CollegeSelectBind() {
    let selectChild = collegeSelectBind_InnerHtml();

    document.getElementById("create-college").innerHTML = selectChild;
    document.getElementById("update-college").innerHTML = selectChild;
    document.getElementById("set-college").innerHTML = selectChild;
    document.getElementById("unset-college").innerHTML = selectChild;
    document.getElementById("delete-college").innerHTML = selectChild;
}

function classManage_MajorSelectBind() {
    let collegeUUID = document.getElementById("create-college").value;
    document.getElementById("create-major").innerHTML = majorSelectBind_InnerHtml(collegeUUID);
    document.getElementById("create-college").onblur = function () {
        let collegeUUID = document.getElementById("create-college").value;
        document.getElementById("create-major").innerHTML = majorSelectBind_InnerHtml(collegeUUID);
    };

    collegeUUID = document.getElementById("update-college").value;
    document.getElementById("update-major").innerHTML = majorSelectBind_InnerHtml(collegeUUID);
    document.getElementById("update-college").onblur = function () {
        let collegeUUID = document.getElementById("update-college").value;
        document.getElementById("update-major").innerHTML = majorSelectBind_InnerHtml(collegeUUID);
    };

    collegeUUID = document.getElementById("set-college").value;
    document.getElementById("set-major").innerHTML = majorSelectBind_InnerHtml(collegeUUID);
    document.getElementById("set-college").onblur = function () {
        let collegeUUID = document.getElementById("set-college").value;
        document.getElementById("set-major").innerHTML = majorSelectBind_InnerHtml(collegeUUID);
    };

    collegeUUID = document.getElementById("unset-college").value;
    document.getElementById("unset-major").innerHTML = majorSelectBind_InnerHtml(collegeUUID);
    document.getElementById("unset-college").onblur = function () {
        let collegeUUID = document.getElementById("unset-college").value;
        document.getElementById("unset-major").innerHTML = majorSelectBind_InnerHtml(collegeUUID);
    };

    collegeUUID = document.getElementById("delete-college").value;
    document.getElementById("delete-major").innerHTML = majorSelectBind_InnerHtml(collegeUUID);
    document.getElementById("delete-college").onblur = function () {
        let collegeUUID = document.getElementById("delete-college").value;
        document.getElementById("delete-major").innerHTML = majorSelectBind_InnerHtml(collegeUUID);
    };
}

function classManage_ClassSelectBind() {
    let majorUUID = document.getElementById("update-major").value;
    document.getElementById("update-class").innerHTML = classSelectBind_InnerHtml(majorUUID);
    document.getElementById("update-major").onblur = function () {
        let majorUUID = document.getElementById("update-major").value;
        document.getElementById("update-class").innerHTML = classSelectBind_InnerHtml(majorUUID);
    };

    majorUUID = document.getElementById("set-major").value;
    document.getElementById("set-class").innerHTML = classSelectBind_InnerHtml(majorUUID);
    document.getElementById("set-major").onblur = function () {
        let majorUUID = document.getElementById("set-major").value;
        document.getElementById("set-class").innerHTML = classSelectBind_InnerHtml(majorUUID);
    };

    majorUUID = document.getElementById("unset-major").value;
    document.getElementById("unset-class").innerHTML = classSelectBind_InnerHtml(majorUUID);
    document.getElementById("unset-major").onblur = function () {
        let majorUUID = document.getElementById("unset-major").value;
        document.getElementById("unset-class").innerHTML = classSelectBind_InnerHtml(majorUUID);
    };

    majorUUID = document.getElementById("delete-major").value;
    let i = classSelectBind_InnerHtml(majorUUID);
    document.getElementById("delete-class").innerHTML = i;
    document.getElementById("delete-verifyClass").innerHTML = i;
    document.getElementById("delete-major").onblur = function () {
        let majorUUID = document.getElementById("delete-major").value;
        let i = classSelectBind_InnerHtml(majorUUID);
        document.getElementById("delete-class").innerHTML = i;
        document.getElementById("delete-verifyClass").innerHTML = i;
    };
}

function courseManage_CollegeSelectBind() {
    let selectChild = collegeSelectBind_InnerHtml();

    document.getElementById("create-college").innerHTML = selectChild;
    document.getElementById("update-college").innerHTML = selectChild;
    document.getElementById("set-college").innerHTML = selectChild;
    document.getElementById("unset-college").innerHTML = selectChild;
    document.getElementById("delete-college").innerHTML = selectChild;
    document.getElementById("choose-college").innerHTML = selectChild;
}

function courseManage_MajorSelectBind() {
    let collegeUUID = document.getElementById("create-college").value;
    document.getElementById("create-major").innerHTML = majorSelectBind_InnerHtml(collegeUUID);
    document.getElementById("create-college").onblur = function () {
        let collegeUUID = document.getElementById("create-college").value;
        document.getElementById("create-major").innerHTML = majorSelectBind_InnerHtml(collegeUUID);
    };

    collegeUUID = document.getElementById("update-college").value;
    document.getElementById("update-major").innerHTML = majorSelectBind_InnerHtml(collegeUUID);
    document.getElementById("update-college").onblur = function () {
        let collegeUUID = document.getElementById("update-college").value;
        document.getElementById("update-major").innerHTML = majorSelectBind_InnerHtml(collegeUUID);
    };

    collegeUUID = document.getElementById("set-college").value;
    document.getElementById("set-major").innerHTML = majorSelectBind_InnerHtml(collegeUUID);
    document.getElementById("set-college").onblur = function () {
        let collegeUUID = document.getElementById("set-college").value;
        document.getElementById("set-major").innerHTML = majorSelectBind_InnerHtml(collegeUUID);
    };

    collegeUUID = document.getElementById("unset-college").value;
    document.getElementById("unset-major").innerHTML = majorSelectBind_InnerHtml(collegeUUID);
    document.getElementById("unset-college").onblur = function () {
        let collegeUUID = document.getElementById("unset-college").value;
        document.getElementById("unset-major").innerHTML = majorSelectBind_InnerHtml(collegeUUID);
    };

    collegeUUID = document.getElementById("delete-college").value;
    document.getElementById("delete-major").innerHTML = majorSelectBind_InnerHtml(collegeUUID);
    document.getElementById("delete-college").onblur = function () {
        let collegeUUID = document.getElementById("delete-college").value;
        document.getElementById("delete-major").innerHTML = majorSelectBind_InnerHtml(collegeUUID);
    };

    collegeUUID = document.getElementById("choose-college").value;
    document.getElementById("choose-major").innerHTML = majorSelectBind_InnerHtml(collegeUUID);
    document.getElementById("choose-college").onblur = function () {
        let collegeUUID = document.getElementById("choose-college").value;
        document.getElementById("choose-major").innerHTML = majorSelectBind_InnerHtml(collegeUUID);
    };
}

function courseManage_CourseSelectBind() {
    let majorUUID = document.getElementById("update-major").value;
    document.getElementById("update-course").innerHTML = courseSelectBind_InnerHtml(majorUUID);
    document.getElementById("update-major").onblur = function () {
        let majorUUID = document.getElementById("update-major").value;
        document.getElementById("update-course").innerHTML = courseSelectBind_InnerHtml(majorUUID);
    };

    majorUUID = document.getElementById("set-major").value;
    document.getElementById("set-course").innerHTML = courseSelectBind_InnerHtml(majorUUID);
    document.getElementById("set-major").onblur = function () {
        let majorUUID = document.getElementById("set-major").value;
        document.getElementById("set-course").innerHTML = courseSelectBind_InnerHtml(majorUUID);
    };

    majorUUID = document.getElementById("unset-major").value;
    document.getElementById("unset-course").innerHTML = courseSelectBind_InnerHtml(majorUUID);
    document.getElementById("unset-major").onblur = function () {
        let majorUUID = document.getElementById("unset-major").value;
        document.getElementById("unset-course").innerHTML = courseSelectBind_InnerHtml(majorUUID);
    };

    majorUUID = document.getElementById("delete-major").value;
    let i = courseSelectBind_InnerHtml(majorUUID);
    document.getElementById("delete-course").innerHTML = i;
    document.getElementById("delete-verifyCourse").innerHTML = i;
    document.getElementById("delete-major").onblur = function () {
        let majorUUID = document.getElementById("delete-major").value;
        let i = courseSelectBind_InnerHtml(majorUUID);
        document.getElementById("delete-course").innerHTML = i;
        document.getElementById("delete-verifyCourse").innerHTML = i;
    };

    majorUUID = document.getElementById("choose-major").value;
    document.getElementById("choose-course").innerHTML = courseSelectBind_InnerHtml(majorUUID);
    document.getElementById("choose-major").onblur = function () {
        let majorUUID = document.getElementById("choose-major").value;
        document.getElementById("choose-course").innerHTML = courseSelectBind_InnerHtml(majorUUID);
    };
}

function questionManage_CollegeSelectBind() {
    document.getElementById("choose-college").innerHTML = collegeSelectBind_InnerHtml();
}

function questionManage_MajorSelectBind() {
    let collegeUUID = document.getElementById("choose-college").value;
    document.getElementById("choose-major").innerHTML = majorSelectBind_InnerHtml(collegeUUID);
    document.getElementById("choose-college").onblur = function () {
        let collegeUUID = document.getElementById("choose-college").value;
        document.getElementById("choose-major").innerHTML = majorSelectBind_InnerHtml(collegeUUID);
    };
}

function questionManage_CourseSelectBind() {
    let majorUUID = document.getElementById("choose-major").value;
    document.getElementById("choose-course").innerHTML = courseSelectBind_InnerHtml(majorUUID);
    document.getElementById("choose-major").onblur = function () {
        let majorUUID = document.getElementById("choose-major").value;
        document.getElementById("choose-course").innerHTML = courseSelectBind_InnerHtml(majorUUID);
    };
}

// gen inner html

function collegeSelectBind_InnerHtml() {
    let listCollegesResponse = window.Action_CollegeStruct_List();
    if (listCollegesResponse === null) {
        alert("网络错误");
        return;
    }
    if (listCollegesResponse.Status.Code !== "Suc") {
        alert(listCollegesResponse.Status.Message);
        return;
    }
    let colleges = listCollegesResponse.Colleges;

    let selectChild = "";
    let tmp = '<option value="{0}">{1}</option>';
    for (let i = 0; i < colleges.length; i++) {
        selectChild += tools.StrFormat(tmp, [
            colleges[i].UUID,
            colleges[i].Name,
        ]);
    }

    return selectChild;
}

function majorSelectBind_InnerHtml(uuid) {
    let listMajorsResponse = window.Action_DisciplineStruct_ListCollege(uuid);
    if (listMajorsResponse === null) {
        alert("网络错误");
        return;
    }
    if (listMajorsResponse.Status.Code !== "Suc") {
        alert(listMajorsResponse.Status.Message);
        return;
    }
    let majors = listMajorsResponse.Disciplines;

    let selectChild = "";
    let tmp = '<option value="{0}">{1}</option>';
    for (let i = 0; i < majors.length; i++) {
        selectChild += tools.StrFormat(tmp, [
            majors[i].UUID,
            majors[i].Name,
        ]);
    }

    return selectChild;
}

function classSelectBind_InnerHtml(uuid) {
    let listClassResponse = window.Action_classStruct_ListMajor(uuid);
    if (listClassResponse === null) {
        alert("网络错误");
        return;
    }
    if (listClassResponse.Status.Code !== "Suc") {
        alert(listClassResponse.Status.Message);
        return;
    }
    let classes = listClassResponse.Class;

    let selectChild = "";
    let tmp = '<option value="{0}">{1}</option>';
    for (let i = 0; i < classes.length; i++) {
        selectChild += tools.StrFormat(tmp, [
            classes[i].UUID,
            classes[i].Name,
        ]);
    }

    return selectChild;
}

function courseSelectBind_InnerHtml(uuid) {
    let listCourseResponse = window.Action_course_ListMajor(uuid);
    if (listCourseResponse === null) {
        alert("网络错误");
        return;
    }
    if (listCourseResponse.Status.Code !== "Suc") {
        alert(listCourseResponse.Status.Message);
        return;
    }
    let courses = listCourseResponse.Course;

    let selectChild = "";
    let tmp = '<option value="{0}">{1}</option>';
    for (let i = 0; i < courses.length; i++) {
        selectChild += tools.StrFormat(tmp, [
            courses[i].UUID,
            courses[i].Name,
        ]);
    }

    return selectChild;
}

// user script action

// college

function createCollege() {
    document.getElementById("create-btn").onclick = function () {
        let name = document.getElementById("create-name").value;
        let description = document.getElementById("create-description").value;

        document.getElementById("create-btn").setAttribute("disabled", "");

        let response = window.Action_CollegeStruct_Create(name, description);
        if (response === null) {
            alert("网络错误");
            document.getElementById("create-btn").removeAttribute("disabled");
            return;
        }
        if (response.Status.Code !== "Suc") {
            alert(response.Status.Message);
            document.getElementById("create-btn").removeAttribute("disabled");
            return;
        }

        alert("创建成功");
        document.getElementById("create-name").setAttribute("disabled", "");
        document.getElementById("create-description").setAttribute("disabled", "");
    }
}

function updateCollege() {
    document.getElementById("update-btn").onclick = function () {
        let uuid = document.getElementById("update-college").value;
        let name = document.getElementById("update-name").value;
        let description = document.getElementById("update-description").value;

        document.getElementById("update-btn").setAttribute("disabled", "");

        let response = window.Action_CollegeStruct_Update(uuid, name, description);
        if (response === null) {
            alert("网络错误");
            document.getElementById("update-btn").removeAttribute("disabled");
            return;
        }
        if (response.Status.Code !== "Suc") {
            alert(response.Status.Message);
            document.getElementById("update-btn").removeAttribute("disabled");
            return;
        }

        alert("更新成功");
        document.getElementById("update-college").setAttribute("disabled", "");
        document.getElementById("update-name").setAttribute("disabled", "");
        document.getElementById("update-description").setAttribute("disabled", "");
    }
}

function setCollegeManager() {
    document.getElementById("set-btn").onclick = function () {
        let uuid = document.getElementById("set-college").value;
        let account = document.getElementById("set-account").value;
        let accountVerify = document.getElementById("set-accountVerify").value;
        if (account !== accountVerify) {
            alert("请确认管理员学工号");
            return;
        }

        document.getElementById("set-btn").setAttribute("disabled", "");

        let getUserUUIDResponse = window.Action_UserInfo_GetUUID(account);
        if (getUserUUIDResponse === null) {
            alert("网络错误");
            document.getElementById("set-btn").removeAttribute("disabled");
            return;
        }
        if (getUserUUIDResponse.Status.Code !== "Suc") {
            alert(getUserUUIDResponse.Status.Message);
            document.getElementById("set-btn").removeAttribute("disabled");
            return;
        }
        let userUUID = getUserUUIDResponse.UUID;

        let response = window.Action_CollegeStruct_SetManager(uuid, userUUID);
        if (response === null) {
            alert("网络错误");
            document.getElementById("set-btn").removeAttribute("disabled");
            return;
        }
        if (response.Status.Code !== "Suc") {
            alert(response.Status.Message);
            document.getElementById("set-btn").removeAttribute("disabled");
            return;
        }

        alert("设置成功");
        document.getElementById("set-college").setAttribute("disabled", "");
        document.getElementById("set-account").setAttribute("disabled", "");
        document.getElementById("set-accountVerify").setAttribute("disabled", "");
    }
}

function unsetCollegeManager() {
    document.getElementById("unset-btn").onclick = function () {
        let uuid = document.getElementById("unset-college").value;
        let account = document.getElementById("unset-account").value;
        let accountVerify = document.getElementById("unset-accountVerify").value;
        if (account !== accountVerify) {
            alert("请确认管理员学工号");
            return;
        }

        document.getElementById("unset-btn").setAttribute("disabled", "");

        let getUserUUIDResponse = window.Action_UserInfo_GetUUID(account);
        if (getUserUUIDResponse === null) {
            alert("网络错误");
            document.getElementById("unset-btn").removeAttribute("disabled");
            return;
        }
        if (getUserUUIDResponse.Status.Code !== "Suc") {
            alert(getUserUUIDResponse.Status.Message);
            document.getElementById("unset-btn").removeAttribute("disabled");
            return;
        }
        let userUUID = getUserUUIDResponse.UUID;

        let response = window.Action_CollegeStruct_UnSetManager(uuid, userUUID);
        if (response === null) {
            alert("网络错误");
            document.getElementById("unset-btn").removeAttribute("disabled");
            return;
        }
        if (response.Status.Code !== "Suc") {
            alert(response.Status.Message);
            document.getElementById("unset-btn").removeAttribute("disabled");
            return;
        }

        alert("移除成功");
        document.getElementById("unset-college").setAttribute("disabled", "");
        document.getElementById("unset-account").setAttribute("disabled", "");
        document.getElementById("unset-accountVerify").setAttribute("disabled", "");
    }
}

function deleteCollege() {
    document.getElementById("delete-btn").onclick = function () {
        let college = document.getElementById("delete-college").value;
        let collegeVerify = document.getElementById("delete-verifyCollege").value;
        if (college !== collegeVerify) {
            alert("请确认学院");
            return;
        }

        document.getElementById("delete-btn").setAttribute("disabled", "");

        let response = window.Action_CollegeStruct_Delete(college);
        if (response === null) {
            alert("网络错误");
            document.getElementById("delete-btn").removeAttribute("disabled");
            return;
        }
        if (response.Status.Code !== "Suc") {
            alert(response.Status.Message);
            document.getElementById("delete-btn").removeAttribute("disabled");
            return;
        }

        alert("删除成功");
        document.getElementById("delete-college").setAttribute("disabled", "");
        document.getElementById("delete-verifyCollege").setAttribute("disabled", "");
    }
}

// major

function createMajor() {
    document.getElementById("create-btn").onclick = function () {
        let college = document.getElementById("create-college").value;
        let name = document.getElementById("create-name").value;
        let description = document.getElementById("create-description").value;

        document.getElementById("create-btn").setAttribute("disabled", "");

        let response = window.Action_DisciplineStruct_Create(college, name, description);
        if (response === null) {
            alert("网络错误");
            document.getElementById("create-btn").removeAttribute("disabled");
            return;
        }
        if (response.Status.Code !== "Suc") {
            alert(response.Status.Message);
            document.getElementById("create-btn").removeAttribute("disabled");
            return;
        }

        alert("创建成功");
        document.getElementById("create-college").setAttribute("disabled", "");
        document.getElementById("create-name").setAttribute("disabled", "");
        document.getElementById("create-description").setAttribute("disabled", "");
    }
}

function updateMajor() {
    document.getElementById("update-btn").onclick = function () {
        let uuid = document.getElementById("update-major").value;
        let name = document.getElementById("update-name").value;
        let description = document.getElementById("update-description").value;

        document.getElementById("update-btn").setAttribute("disabled", "");

        let response = window.Action_DisciplineStruct_Update(uuid, name, description);
        if (response === null) {
            alert("网络错误");
            document.getElementById("update-btn").removeAttribute("disabled");
            return;
        }
        if (response.Status.Code !== "Suc") {
            alert(response.Status.Message);
            document.getElementById("update-btn").removeAttribute("disabled");
            return;
        }

        alert("更新成功");
        document.getElementById("update-college").setAttribute("disabled", "");
        document.getElementById("update-major").setAttribute("disabled", "");
        document.getElementById("update-name").setAttribute("disabled", "");
        document.getElementById("update-description").setAttribute("disabled", "");
    }
}

function setMajorManager() {
    document.getElementById("set-btn").onclick = function () {
        let major = document.getElementById("set-major").value;
        let account = document.getElementById("set-account").value;
        let accountVerify = document.getElementById("set-accountVerify").value;
        if (account !== accountVerify) {
            alert("请确认管理员学工号");
            return;
        }

        document.getElementById("set-btn").setAttribute("disabled", "");

        let getUserUUIDResponse = window.Action_UserInfo_GetUUID(account);
        if (getUserUUIDResponse === null) {
            alert("网络错误");
            document.getElementById("set-btn").removeAttribute("disabled");
            return;
        }
        if (getUserUUIDResponse.Status.Code !== "Suc") {
            alert(getUserUUIDResponse.Status.Message);
            document.getElementById("set-btn").removeAttribute("disabled");
            return;
        }
        let userUUID = getUserUUIDResponse.UUID;

        let response = window.Action_DisciplineStruct_SetManager(major, userUUID);
        if (response === null) {
            alert("网络错误");
            document.getElementById("set-btn").removeAttribute("disabled");
            return;
        }
        if (response.Status.Code !== "Suc") {
            alert(response.Status.Message);
            document.getElementById("set-btn").removeAttribute("disabled");
            return;
        }

        alert("设置成功");
        document.getElementById("set-college").setAttribute("disabled", "");
        document.getElementById("set-major").setAttribute("disabled", "");
        document.getElementById("set-account").setAttribute("disabled", "");
        document.getElementById("set-accountVerify").setAttribute("disabled", "");
    }
}

function unsetMajorManager() {
    document.getElementById("unset-btn").onclick = function () {
        let major = document.getElementById("unset-major").value;
        let account = document.getElementById("unset-account").value;
        let accountVerify = document.getElementById("unset-accountVerify").value;
        if (account !== accountVerify) {
            alert("请确认管理员学工号");
            return;
        }

        document.getElementById("unset-btn").setAttribute("disabled", "");

        let getUserUUIDResponse = window.Action_UserInfo_GetUUID(account);
        if (getUserUUIDResponse === null) {
            alert("网络错误");
            document.getElementById("unset-btn").removeAttribute("disabled");
            return;
        }
        if (getUserUUIDResponse.Status.Code !== "Suc") {
            alert(getUserUUIDResponse.Status.Message);
            document.getElementById("unset-btn").removeAttribute("disabled");
            return;
        }
        let userUUID = getUserUUIDResponse.UUID;

        let response = window.Action_DisciplineStruct_UnSetManager(major, userUUID);
        if (response === null) {
            alert("网络错误");
            document.getElementById("unset-btn").removeAttribute("disabled");
            return;
        }
        if (response.Status.Code !== "Suc") {
            alert(response.Status.Message);
            document.getElementById("unset-btn").removeAttribute("disabled");
            return;
        }

        alert("移除成功");
        document.getElementById("unset-college").setAttribute("disabled", "");
        document.getElementById("unset-major").setAttribute("disabled", "");
        document.getElementById("unset-account").setAttribute("disabled", "");
        document.getElementById("unset-accountVerify").setAttribute("disabled", "");
    }
}

function deleteMajor() {
    document.getElementById("delete-btn").onclick = function () {
        let major = document.getElementById("delete-major").value;
        let majorVerify = document.getElementById("delete-verifyMajor").value;
        if (major !== majorVerify) {
            alert("请确认专业");
            return;
        }

        document.getElementById("delete-btn").setAttribute("disabled", "");

        let response = window.Action_DisciplineStruct_Delete(major);
        if (response === null) {
            alert("网络错误");
            document.getElementById("delete-btn").removeAttribute("disabled");
            return;
        }
        if (response.Status.Code !== "Suc") {
            alert(response.Status.Message);
            document.getElementById("delete-btn").removeAttribute("disabled");
            return;
        }

        alert("删除成功");
        document.getElementById("delete-college").setAttribute("disabled", "");
        document.getElementById("delete-major").setAttribute("disabled", "");
        document.getElementById("delete-verifyMajor").setAttribute("disabled", "");
    }
}

// class

function createClass() {
    document.getElementById("create-btn").onclick = function () {
        let major = document.getElementById("create-major").value;
        let name = document.getElementById("create-name").value;
        let description = document.getElementById("create-description").value;

        document.getElementById("create-btn").setAttribute("disabled", "");

        let response = window.Action_classStruct_Create(major, name, description);
        if (response === null) {
            alert("网络错误");
            document.getElementById("create-btn").removeAttribute("disabled");
            return;
        }
        if (response.Status.Code !== "Suc") {
            alert(response.Status.Message);
            document.getElementById("create-btn").removeAttribute("disabled");
            return;
        }

        alert("创建成功");
        document.getElementById("create-college").setAttribute("disabled", "");
        document.getElementById("create-major").setAttribute("disabled", "");
        document.getElementById("create-name").setAttribute("disabled", "");
        document.getElementById("create-description").setAttribute("disabled", "");
    }
}

function updateClass() {
    document.getElementById("update-btn").onclick = function () {
        let uuid = document.getElementById("update-class").value;
        let name = document.getElementById("update-name").value;
        let description = document.getElementById("update-description").value;

        document.getElementById("update-btn").setAttribute("disabled", "");

        let response = window.Action_classStruct_Update(uuid, name, description);
        if (response === null) {
            alert("网络错误");
            document.getElementById("update-btn").removeAttribute("disabled");
            return;
        }
        if (response.Status.Code !== "Suc") {
            alert(response.Status.Message);
            document.getElementById("update-btn").removeAttribute("disabled");
            return;
        }

        alert("更新成功");
        document.getElementById("update-college").setAttribute("disabled", "");
        document.getElementById("update-major").setAttribute("disabled", "");
        document.getElementById("update-class").setAttribute("disabled", "");
        document.getElementById("update-name").setAttribute("disabled", "");
        document.getElementById("update-description").setAttribute("disabled", "");
    }
}

function setClassManager() {
    document.getElementById("set-btn").onclick = function () {
        let classUUID = document.getElementById("set-class").value;
        let account = document.getElementById("set-account").value;
        let accountVerify = document.getElementById("set-accountVerify").value;
        if (account !== accountVerify) {
            alert("请确认管理员学工号");
            return;
        }

        document.getElementById("set-btn").setAttribute("disabled", "");

        let getUserUUIDResponse = window.Action_UserInfo_GetUUID(account);
        if (getUserUUIDResponse === null) {
            alert("网络错误");
            document.getElementById("set-btn").removeAttribute("disabled");
            return;
        }
        if (getUserUUIDResponse.Status.Code !== "Suc") {
            alert(getUserUUIDResponse.Status.Message);
            document.getElementById("set-btn").removeAttribute("disabled");
            return;
        }
        let userUUID = getUserUUIDResponse.UUID;

        let response = window.Action_classStruct_SetManager(classUUID, userUUID);
        if (response === null) {
            alert("网络错误");
            document.getElementById("set-btn").removeAttribute("disabled");
            return;
        }
        if (response.Status.Code !== "Suc") {
            alert(response.Status.Message);
            document.getElementById("set-btn").removeAttribute("disabled");
            return;
        }

        alert("设置成功");
        document.getElementById("set-college").setAttribute("disabled", "");
        document.getElementById("set-major").setAttribute("disabled", "");
        document.getElementById("set-class").setAttribute("disabled", "");
        document.getElementById("set-account").setAttribute("disabled", "");
        document.getElementById("set-accountVerify").setAttribute("disabled", "");
    }
}

function unsetClassManager() {
    document.getElementById("unset-btn").onclick = function () {
        let classUUID = document.getElementById("unset-class").value;
        let account = document.getElementById("unset-account").value;
        let accountVerify = document.getElementById("unset-accountVerify").value;
        if (account !== accountVerify) {
            alert("请确认管理员学工号");
            return;
        }

        document.getElementById("unset-btn").setAttribute("disabled", "");

        let getUserUUIDResponse = window.Action_UserInfo_GetUUID(account);
        if (getUserUUIDResponse === null) {
            alert("网络错误");
            document.getElementById("unset-btn").removeAttribute("disabled");
            return;
        }
        if (getUserUUIDResponse.Status.Code !== "Suc") {
            alert(getUserUUIDResponse.Status.Message);
            document.getElementById("unset-btn").removeAttribute("disabled");
            return;
        }
        let userUUID = getUserUUIDResponse.UUID;

        let response = window.Action_classStruct_UnSetManager(classUUID, userUUID);
        if (response === null) {
            alert("网络错误");
            document.getElementById("unset-btn").removeAttribute("disabled");
            return;
        }
        if (response.Status.Code !== "Suc") {
            alert(response.Status.Message);
            document.getElementById("unset-btn").removeAttribute("disabled");
            return;
        }

        alert("移除成功");
        document.getElementById("unset-college").setAttribute("disabled", "");
        document.getElementById("unset-major").setAttribute("disabled", "");
        document.getElementById("unset-class").setAttribute("disabled", "");
        document.getElementById("unset-account").setAttribute("disabled", "");
        document.getElementById("unset-accountVerify").setAttribute("disabled", "");
    }
}

function deleteClass() {
    document.getElementById("delete-btn").onclick = function () {
        let classUUID = document.getElementById("delete-class").value;
        let classUUIDVerify = document.getElementById("delete-verifyClass").value;
        if (classUUID !== classUUIDVerify) {
            alert("请确认班级");
            return;
        }

        document.getElementById("delete-btn").setAttribute("disabled", "");

        let response = window.Action_classStruct_Delete(classUUID);
        if (response === null) {
            alert("网络错误");
            document.getElementById("delete-btn").removeAttribute("disabled");
            return;
        }
        if (response.Status.Code !== "Suc") {
            alert(response.Status.Message);
            document.getElementById("delete-btn").removeAttribute("disabled");
            return;
        }

        alert("删除成功");
        document.getElementById("delete-college").setAttribute("disabled", "");
        document.getElementById("delete-major").setAttribute("disabled", "");
        document.getElementById("delete-class").setAttribute("disabled", "");
        document.getElementById("delete-verifyClass").setAttribute("disabled", "");
    }
}

function createCourse() {
    document.getElementById("create-btn").onclick = function () {
        let major = document.getElementById("create-major").value;
        let name = document.getElementById("create-name").value;
        let description = document.getElementById("create-description").value;

        document.getElementById("create-btn").setAttribute("disabled", "");

        let response = window.Action_course_Create(major, name, description);
        if (response === null) {
            alert("网络错误");
            document.getElementById("create-btn").removeAttribute("disabled");
            return;
        }
        if (response.Status.Code !== "Suc") {
            alert(response.Status.Message);
            document.getElementById("create-btn").removeAttribute("disabled");
            return;
        }

        alert("创建成功");
        document.getElementById("create-college").setAttribute("disabled", "");
        document.getElementById("create-major").setAttribute("disabled", "");
        document.getElementById("create-name").setAttribute("disabled", "");
        document.getElementById("create-description").setAttribute("disabled", "");
    }
}

function updateCourse() {
    document.getElementById("update-btn").onclick = function () {
        let uuid = document.getElementById("update-course").value;
        let name = document.getElementById("update-name").value;
        let description = document.getElementById("update-description").value;

        document.getElementById("update-btn").setAttribute("disabled", "");

        let response = window.Action_course_Update(uuid, name, description);
        if (response === null) {
            alert("网络错误");
            document.getElementById("update-btn").removeAttribute("disabled");
            return;
        }
        if (response.Status.Code !== "Suc") {
            alert(response.Status.Message);
            document.getElementById("update-btn").removeAttribute("disabled");
            return;
        }

        alert("更新成功");
        document.getElementById("update-college").setAttribute("disabled", "");
        document.getElementById("update-major").setAttribute("disabled", "");
        document.getElementById("update-course").setAttribute("disabled", "");
        document.getElementById("update-name").setAttribute("disabled", "");
        document.getElementById("update-description").setAttribute("disabled", "");
    }
}

// course

function setCourseManager() {
    document.getElementById("set-btn").onclick = function () {
        let courseUUID = document.getElementById("set-course").value;
        let account = document.getElementById("set-account").value;
        let accountVerify = document.getElementById("set-accountVerify").value;
        if (account !== accountVerify) {
            alert("请确认管理员学工号");
            return;
        }

        document.getElementById("set-btn").setAttribute("disabled", "");

        let getUserUUIDResponse = window.Action_UserInfo_GetUUID(account);
        if (getUserUUIDResponse === null) {
            alert("网络错误");
            document.getElementById("set-btn").removeAttribute("disabled");
            return;
        }
        if (getUserUUIDResponse.Status.Code !== "Suc") {
            alert(getUserUUIDResponse.Status.Message);
            document.getElementById("set-btn").removeAttribute("disabled");
            return;
        }
        let userUUID = getUserUUIDResponse.UUID;

        let response = window.Action_course_SetManager(courseUUID, userUUID);
        if (response === null) {
            alert("网络错误");
            document.getElementById("set-btn").removeAttribute("disabled");
            return;
        }
        if (response.Status.Code !== "Suc") {
            alert(response.Status.Message);
            document.getElementById("set-btn").removeAttribute("disabled");
            return;
        }

        alert("设置成功");
        document.getElementById("set-college").setAttribute("disabled", "");
        document.getElementById("set-major").setAttribute("disabled", "");
        document.getElementById("set-course").setAttribute("disabled", "");
        document.getElementById("set-account").setAttribute("disabled", "");
        document.getElementById("set-accountVerify").setAttribute("disabled", "");
    }
}

function unsetCourseManager() {
    document.getElementById("unset-btn").onclick = function () {
        let courseUUID = document.getElementById("unset-course").value;
        let account = document.getElementById("unset-account").value;
        let accountVerify = document.getElementById("unset-accountVerify").value;
        if (account !== accountVerify) {
            alert("请确认管理员学工号");
            return;
        }

        document.getElementById("unset-btn").setAttribute("disabled", "");

        let getUserUUIDResponse = window.Action_UserInfo_GetUUID(account);
        if (getUserUUIDResponse === null) {
            alert("网络错误");
            document.getElementById("unset-btn").removeAttribute("disabled");
            return;
        }
        if (getUserUUIDResponse.Status.Code !== "Suc") {
            alert(getUserUUIDResponse.Status.Message);
            document.getElementById("unset-btn").removeAttribute("disabled");
            return;
        }
        let userUUID = getUserUUIDResponse.UUID;

        let response = window.Action_course_UnSetManager(courseUUID, userUUID);
        if (response === null) {
            alert("网络错误");
            document.getElementById("unset-btn").removeAttribute("disabled");
            return;
        }
        if (response.Status.Code !== "Suc") {
            alert(response.Status.Message);
            document.getElementById("unset-btn").removeAttribute("disabled");
            return;
        }

        alert("移除成功");
        document.getElementById("unset-college").setAttribute("disabled", "");
        document.getElementById("unset-major").setAttribute("disabled", "");
        document.getElementById("unset-course").setAttribute("disabled", "");
        document.getElementById("unset-account").setAttribute("disabled", "");
        document.getElementById("unset-accountVerify").setAttribute("disabled", "");
    }
}

function deleteCourse() {
    document.getElementById("delete-btn").onclick = function () {
        let courseUUID = document.getElementById("delete-course").value;
        let courseUUIDVerify = document.getElementById("delete-verifyCourse").value;
        if (courseUUID !== courseUUIDVerify) {
            alert("请确认课程");
            return;
        }

        document.getElementById("delete-btn").setAttribute("disabled", "");

        let response = window.Action_course_Delete(courseUUID);
        if (response === null) {
            alert("网络错误");
            document.getElementById("delete-btn").removeAttribute("disabled");
            return;
        }
        if (response.Status.Code !== "Suc") {
            alert(response.Status.Message);
            document.getElementById("delete-btn").removeAttribute("disabled");
            return;
        }

        alert("删除成功");
        document.getElementById("delete-college").setAttribute("disabled", "");
        document.getElementById("delete-major").setAttribute("disabled", "");
        document.getElementById("delete-course").setAttribute("disabled", "");
        document.getElementById("delete-verifyCourse").setAttribute("disabled", "");
    }
}

// chapter

window.selectCourseUUID = "";
window.chooseChapterUUID = "";
window.selectChapterUUID = "";
window.lastChangeContent = "";

function selectCourse() {
    document.getElementById("choose-btn").onclick = function () {
        window.selectCourseUUID = document.getElementById("choose-course").value;

        renderChapterList();

        alert("选择成功");
        document.getElementById("choose-college").setAttribute("disabled", "");
        document.getElementById("choose-major").setAttribute("disabled", "");
        document.getElementById("choose-course").setAttribute("disabled", "");
        document.getElementById("choose-btn").setAttribute("disabled", "");
    }
}

function renderChapterList() {
    let response = window.Action_chapter_List(window.selectCourseUUID);
    if (response === null) {
        alert("网络错误");
        return;
    }
    if (response.Status.Code !== "Suc") {
        alert(response.Status.Message);
        return;
    }
    let chapters = response.Chapter;

    let listTemplate = '' +
        '<div class="width-full margin-b1 flex flex-row justify-start align-center" style="height: 3.2rem">' +
        '<div class="margin-r1 font-weight-bolder" style="max-width: 2rem; overflow: auto; white-space: nowrap">{0}</div>' +
        '<div class="margin-r1 font-weight-bolder" style="max-width: 8rem; overflow: auto; white-space: nowrap">{1}</div>' +
        '<div class="margin-r1" style="max-width: 64rem; overflow: auto; white-space: nowrap">{2}</div>' +
        '<div class="flex-empty"></div>' +
        '<button name="chapterBtn" data-toggle="modal" data-target="#setChapterModal" type="button" ' +
        'class="margin-r1 btn btn-primary" value="{3}" onclick="window.chooseChapterUUID=this.value">设置管理员</button>' +
        '<button name="chapterBtn" data-toggle="modal" data-target="#unsetChapterModal" type="button" ' +
        'class="margin-r1 btn btn-warning" value="{4}" onclick="window.chooseChapterUUID=this.value">移除管理员</button>' +
        '<button name="chapterBtn" data-toggle="modal" data-target="#deleteChapterModal" type="button" ' +
        'class="margin-r1 btn btn-danger" value="{5}" onclick="window.chooseChapterUUID=this.value">删除此章节</button>' +
        '</div>';
    let selectTemplate = '<option value="{0}">{1}</option>';

    let listInnerHtml = "";
    let selectInnerHtml = "";

    for (let i = 0; i < chapters.length; i++) {
        let uuid = chapters[i].UUID;
        let index = chapters[i].Index;
        let name = chapters[i].Name;
        let description = chapters[i].Description;
        listInnerHtml += tools.StrFormat(listTemplate, [index, name, description, uuid, uuid, uuid]);
        selectInnerHtml += tools.StrFormat(selectTemplate, [uuid, name]);
    }

    document.getElementById("chapterList").innerHTML = listInnerHtml;
    document.getElementById("chapterEdit-choose").innerHTML = selectInnerHtml;
}

function createChapter() {
    document.getElementById("chapter-create-btn-submit").onclick = function () {
        let course = window.selectCourseUUID;
        let name = document.getElementById("chapter-create-name").value;
        let description = document.getElementById("chapter-create-description").value;
        let index = document.getElementById("chapter-create-index").value;

        let response = window.Action_chapter_Create(course, index, name, description);
        if (response === null) {
            alert("网络错误");
            return;
        }
        if (response.Status.Code !== "Suc") {
            alert(response.Status.Message);
            return;
        }

        alert("创建成功");
        document.getElementById("chapter-create-name").value = "";
        document.getElementById("chapter-create-description").value = "";
        document.getElementById("chapter-create-index").value = "";
        $("#createChapterModal").modal('hide');
    }
}

function setChapterManager() {
    document.getElementById("chapter-set-btn-submit").onclick = function () {
        let account = document.getElementById("chapter-set-account").value;
        let accountVerify = document.getElementById("chapter-set-verifyAccount").value;
        if (account !== accountVerify) {
            alert("请确认管理员学工号");
            return;
        }

        let getUserUUIDResponse = window.Action_UserInfo_GetUUID(account);
        if (getUserUUIDResponse === null) {
            alert("网络错误");
            return;
        }
        if (getUserUUIDResponse.Status.Code !== "Suc") {
            alert(getUserUUIDResponse.Status.Message);
            return;
        }
        let userUUID = getUserUUIDResponse.UUID;

        let response = window.Action_chapter_SetManager(window.chooseChapterUUID, userUUID);
        if (response === null) {
            alert("网络错误");
            return;
        }
        if (response.Status.Code !== "Suc") {
            alert(response.Status.Message);
            return;
        }

        alert("设置成功");
        document.getElementById("chapter-set-account").value = "";
        document.getElementById("chapter-set-verifyAccount").value = "";
        $("#setChapterModal").modal('hide');
    }
}

function unsetChapterManager() {
    document.getElementById("chapter-unset-btn-submit").onclick = function () {
        let account = document.getElementById("chapter-unset-account").value;
        let accountVerify = document.getElementById("chapter-unset-verifyAccount").value;
        if (account !== accountVerify) {
            alert("请确认管理员学工号");
            return;
        }

        let getUserUUIDResponse = window.Action_UserInfo_GetUUID(account);
        if (getUserUUIDResponse === null) {
            alert("网络错误");
            return;
        }
        if (getUserUUIDResponse.Status.Code !== "Suc") {
            alert(getUserUUIDResponse.Status.Message);
            return;
        }
        let userUUID = getUserUUIDResponse.UUID;

        let response = window.Action_chapter_UnSetManager(window.chooseChapterUUID, userUUID);
        if (response === null) {
            alert("网络错误");
            return;
        }
        if (response.Status.Code !== "Suc") {
            alert(response.Status.Message);
            return;
        }

        alert("移除成功");
        document.getElementById("chapter-unset-account").value = "";
        document.getElementById("chapter-unset-verifyAccount").value = "";
        $("#unsetChapterModal").modal('hide');
    }
}

function deleteChapter() {
    document.getElementById("chapter-delete-btn-submit").onclick = function () {
        let response = window.Action_chapter_GetChapter(window.chooseChapterUUID);
        if (response === null) {
            alert("网络错误");
            return;
        }
        if (response.Status.Code !== "Suc") {
            alert(response.Status.Message);
            return;
        }
        let name = response.Name;

        let nameVerify = document.getElementById("chapter-delete-name").value;
        if (name !== nameVerify) {
            alert("请确认章节");
            return;
        }

        response = window.Action_chapter_Delete(window.chooseChapterUUID);
        if (response === null) {
            alert("网络错误");
            return;
        }
        if (response.Status.Code !== "Suc") {
            alert(response.Status.Message);
            return;
        }

        alert("删除成功");
        document.getElementById("chapter-delete-name").value = "";
        $("#deleteChapterModal").modal('hide');
    }
}

function selectChapter() {
    document.getElementById("chapter-edit-btn-submit").onclick = function () {
        window.selectChapterUUID = document.getElementById("chapterEdit-choose").value;
        getContent();

        alert("选择成功");
        document.getElementById("chapterEdit-choose-btn").setAttribute("disabled", "");
        $("#chooseChapterToEditModal").modal('hide');
    }
}

function getContent() {
    let response = window.Action_chapter_GetContent(window.selectChapterUUID);
    if (response === null) {
        alert("网络错误");
        return;
    }
    if (response.Status.Code !== "Suc") {
        alert(response.Status.Message);
        return;
    }
    let contentBase64 = response.MarkdownBase64;
    let content = tools.UnBase64(contentBase64);
    window.lastChangeContent = content;
    document.getElementById("chapterEdit-edit").value = content;
}

function saveChapterContent() {
    document.getElementById("chapter-save-btn-submit").onclick = function () {
        let content = document.getElementById("chapterEdit-edit").value;

        let response = window.Action_chapter_UpdateContent(window.selectChapterUUID, content);
        if (response === null) {
            alert("网络错误");
            return;
        }
        if (response.Status.Code !== "Suc") {
            alert(response.Status.Message);
            return;
        }
        alert("保存成功");
        window.lastChangeContent = content;
        $("#chapterEditSaveModal").modal('hide');
    }
}

function rollBackChapterContent() {
    document.getElementById("chapter-rollback-btn-submit").onclick = function () {
        document.getElementById("chapterEdit-edit").value = window.lastChangeContent;
        alert("回滚成功");
        $("#chapterEditRollBackModal").modal('hide');
    }
}

function displayChapterContent() {
    document.getElementById("chapterEdit-display").onclick = function () {
        let markdown = document.getElementById("chapterEdit-edit").value;
        document.getElementById("chapterEditDisplayModal-body").innerHTML = tools.CompileMarkdown(markdown);
    }
}

// questionBank

window.question_selectCourseUUID = "";
window.question_selectBankUUID = "";

function questionBank_selectCourse() {
    document.getElementById("choose-btn").onclick = function () {
        window.question_selectCourseUUID = document.getElementById("choose-course").value;

        renderBankSelect(window.question_selectCourseUUID);

        alert("选择成功");
        document.getElementById("choose-college").setAttribute("disabled", "");
        document.getElementById("choose-major").setAttribute("disabled", "");
        document.getElementById("choose-course").setAttribute("disabled", "");
        document.getElementById("choose-btn").setAttribute("disabled", "");
    }
}

function renderBankSelect(uuid) {
    let listCourseResponse = window.Action_questionBank_List(uuid);
    if (listCourseResponse === null) {
        alert("网络错误");
        return;
    }
    if (listCourseResponse.Status.Code !== "Suc") {
        alert(listCourseResponse.Status.Message);
        return;
    }
    let UUID = listCourseResponse.UUID;
    let name = listCourseResponse.Name;

    if (UUID !== "") {
        document.getElementById("create-name").setAttribute("disabled", "");
        document.getElementById("create-description").setAttribute("disabled", "");
        document.getElementById("create-btn").setAttribute("disabled", "");
    }

    let tmp = '<option value="{0}">{1}</option>';
    let i = tools.StrFormat(tmp, [UUID, name]);

    document.getElementById("update-bank").innerHTML = i;
    document.getElementById("set-bank").innerHTML = i;
    document.getElementById("unset-bank").innerHTML = i;
    document.getElementById("delete-bank").innerHTML = i;
    document.getElementById("delete-verifyBank").innerHTML = i;
    document.getElementById("chooseB-bank").innerHTML = i;
}

function questionBank_createBank() {
    document.getElementById("create-btn").onclick = function () {
        let course = window.question_selectCourseUUID;
        let name = document.getElementById("create-name").value;
        let description = document.getElementById("create-description").value;

        document.getElementById("create-btn").setAttribute("disabled", "");

        let response = window.Action_questionBank_Create(course, name, description);
        if (response === null) {
            alert("网络错误");
            document.getElementById("create-btn").removeAttribute("disabled");
            return;
        }
        if (response.Status.Code !== "Suc") {
            alert(response.Status.Message);
            document.getElementById("create-btn").removeAttribute("disabled");
            return;
        }

        alert("创建成功");
        document.getElementById("create-name").setAttribute("disabled", "");
        document.getElementById("create-description").setAttribute("disabled", "");
    }
}

function questionBank_update() {
    document.getElementById("update-btn").onclick = function () {
        let UUID = document.getElementById("update-bank").value;
        let name = document.getElementById("update-name").value;
        let description = document.getElementById("update-description").value;

        document.getElementById("update-btn").setAttribute("disabled", "");

        let response = window.Action_questionBank_Update(UUID, name, description);
        if (response === null) {
            alert("网络错误");
            document.getElementById("update-btn").removeAttribute("disabled");
            return;
        }
        if (response.Status.Code !== "Suc") {
            alert(response.Status.Message);
            document.getElementById("update-btn").removeAttribute("disabled");
            return;
        }

        alert("更新成功");
        document.getElementById("update-bank").setAttribute("disabled", "");
        document.getElementById("update-name").setAttribute("disabled", "");
        document.getElementById("update-description").setAttribute("disabled", "");
    }
}

function questionBank_setBankManager() {
    document.getElementById("set-btn").onclick = function () {
        let UUID = document.getElementById("set-bank").value;
        let account = document.getElementById("set-account").value;
        let accountVerify = document.getElementById("set-accountVerify").value;
        if (account !== accountVerify) {
            alert("请确认管理员学工号");
            return;
        }

        document.getElementById("set-btn").setAttribute("disabled", "");

        let getUserUUIDResponse = window.Action_UserInfo_GetUUID(account);
        if (getUserUUIDResponse === null) {
            alert("网络错误");
            document.getElementById("set-btn").removeAttribute("disabled");
            return;
        }
        if (getUserUUIDResponse.Status.Code !== "Suc") {
            alert(getUserUUIDResponse.Status.Message);
            document.getElementById("set-btn").removeAttribute("disabled");
            return;
        }
        let userUUID = getUserUUIDResponse.UUID;

        let response = window.Action_questionBank_SetManager(UUID, userUUID);
        if (response === null) {
            alert("网络错误");
            document.getElementById("set-btn").removeAttribute("disabled");
            return;
        }
        if (response.Status.Code !== "Suc") {
            alert(response.Status.Message);
            document.getElementById("set-btn").removeAttribute("disabled");
            return;
        }

        alert("设置成功");
        document.getElementById("set-bank").setAttribute("disabled", "");
        document.getElementById("set-account").setAttribute("disabled", "");
        document.getElementById("set-accountVerify").setAttribute("disabled", "");
    }
}

function questionBank_unsetBankManager() {
    document.getElementById("unset-btn").onclick = function () {
        let UUID = document.getElementById("unset-bank").value;
        let account = document.getElementById("unset-account").value;
        let accountVerify = document.getElementById("unset-accountVerify").value;
        if (account !== accountVerify) {
            alert("请确认管理员学工号");
            return;
        }

        document.getElementById("unset-btn").setAttribute("disabled", "");

        let getUserUUIDResponse = window.Action_UserInfo_GetUUID(account);
        if (getUserUUIDResponse === null) {
            alert("网络错误");
            document.getElementById("unset-btn").removeAttribute("disabled");
            return;
        }
        if (getUserUUIDResponse.Status.Code !== "Suc") {
            alert(getUserUUIDResponse.Status.Message);
            document.getElementById("unset-btn").removeAttribute("disabled");
            return;
        }
        let userUUID = getUserUUIDResponse.UUID;

        let response = window.Action_questionBank_UnSetManager(UUID, userUUID);
        if (response === null) {
            alert("网络错误");
            document.getElementById("unset-btn").removeAttribute("disabled");
            return;
        }
        if (response.Status.Code !== "Suc") {
            alert(response.Status.Message);
            document.getElementById("unset-btn").removeAttribute("disabled");
            return;
        }

        alert("移除成功");
        document.getElementById("unset-bank").setAttribute("disabled", "");
        document.getElementById("unset-account").setAttribute("disabled", "");
        document.getElementById("unset-accountVerify").setAttribute("disabled", "");
    }
}

function questionBank_deleteBank() {
    document.getElementById("delete-btn").onclick = function () {
        let UUID = document.getElementById("delete-bank").value;
        let UUIDVerify = document.getElementById("delete-verifyBank").value;
        if (UUID !== UUIDVerify) {
            alert("请确认题库");
            return;
        }

        document.getElementById("delete-btn").setAttribute("disabled", "");

        let response = window.Action_questionBank_Delete(UUID);
        if (response === null) {
            alert("网络错误");
            document.getElementById("delete-btn").setAttribute("disabled", "");
            return;
        }
        if (response.Status.Code !== "Suc") {
            alert(response.Status.Message);
            document.getElementById("delete-btn").setAttribute("disabled", "");
            return;
        }

        alert("删除成功");
        document.getElementById("delete-bank").setAttribute("disabled", "");
        document.getElementById("delete-verifyBank").setAttribute("disabled", "");
    }
}

function questionBank_selectBank() {
    document.getElementById("chooseB-btn").onclick = function () {
        window.question_selectBankUUID = document.getElementById("chooseB-bank").value;

        renderQuestionList();

        alert("选择成功");
        document.getElementById("chooseB-btn").setAttribute("disabled", "");
        document.getElementById("chooseB-bank").setAttribute("disabled", "");
    }
}

// question

function createSingleQuestion() {
    document.getElementById("questionS-create-btn-submit").onclick = function () {
        let bank = window.question_selectBankUUID;

        let question = document.getElementById("questionS-create-question").value;
        let analysis = document.getElementById("questionS-create-analysis").value;
        let a = document.getElementById("questionS-create-a").value;
        let b = document.getElementById("questionS-create-b").value;
        let c = document.getElementById("questionS-create-c").value;
        let d = document.getElementById("questionS-create-d").value;
        let answer = document.getElementById("questionS-create-answer").value;
        let level = document.getElementById("questionS-create-level").value;

        let isA = false;
        let isB = false;
        let isC = false;
        let isD = false;

        if (answer === "A") {
            isA = true;
        } else if (answer === "B") {
            isB = true;
        } else if (answer === "C") {
            isC = true;
        } else {
            isD = true;
        }

        let response = window.Action_question_Create(question, analysis, a, b, c, d, isA, isB, isC, isD,
            false, window.API_CODE_QuestionTypeCode_Single, level, bank);
        if (response === null || response === undefined) {
            alert("网络错误");
            return;
        }
        if (response.Status.Code !== "Suc") {
            alert(response.Status.Message);
            return;
        }

        alert("创建成功");
        document.getElementById("questionS-create-question").value = "";
        document.getElementById("questionS-create-analysis").value = "";
        document.getElementById("questionS-create-a").value = "";
        document.getElementById("questionS-create-b").value = "";
        document.getElementById("questionS-create-c").value = "";
        document.getElementById("questionS-create-d").value = "";
        $("#createSingleQuestionModal").modal('hide');
    }
}

function createMultipleQuestion() {
    document.getElementById("questionM-create-btn-submit").onclick = function () {
        let bank = window.question_selectBankUUID;

        let question = document.getElementById("questionM-create-question").value;
        let analysis = document.getElementById("questionM-create-analysis").value;
        let a = document.getElementById("questionM-create-a").value;
        let b = document.getElementById("questionM-create-b").value;
        let c = document.getElementById("questionM-create-c").value;
        let d = document.getElementById("questionM-create-d").value;
        let answer = document.getElementById("questionM-create-answer").value;
        let level = document.getElementById("questionM-create-level").value;

        let isA = false;
        let isB = false;
        let isC = false;
        let isD = false;

        if (answer === "A") {
            isA = true;
        }
        if (answer === "B") {
            isB = true;
        }
        if (answer === "C") {
            isC = true;
        }
        if (answer === "D") {
            isD = true;
        }

        let response = window.Action_question_Create(question, analysis, a, b, c, d, isA, isB, isC, isD,
            false, window.API_CODE_QuestionTypeCode_Multiple, level, bank);
        if (response === null || response === undefined) {
            alert("网络错误");
            return;
        }
        if (response.Status.Code !== "Suc") {
            alert(response.Status.Message);
            return;
        }

        alert("创建成功");
        document.getElementById("questionM-create-question").value = "";
        document.getElementById("questionM-create-analysis").value = "";
        document.getElementById("questionM-create-a").value = "";
        document.getElementById("questionM-create-b").value = "";
        document.getElementById("questionM-create-c").value = "";
        document.getElementById("questionM-create-d").value = "";
        $("#createMultipleQuestionModal").modal('hide');
    }
}

function createJudgeQuestion() {
    document.getElementById("questionJ-create-btn-submit").onclick = function () {
        let bank = window.question_selectBankUUID;

        let question = document.getElementById("questionJ-create-question").value;
        let analysis = document.getElementById("questionJ-create-analysis").value;
        let answer = document.getElementById("questionJ-create-answer").value;
        let level = document.getElementById("questionJ-create-level").value;

        let a = false;
        a = answer === "1";

        let response = window.Action_question_Create(question, analysis,
            "", "", "", "", false, false, false, false, a,
            window.API_CODE_QuestionTypeCode_Judge, level, bank);
        if (response === null || response === undefined) {
            alert("网络错误");
            return;
        }
        if (response.Status.Code !== "Suc") {
            alert(response.Status.Message);
            return;
        }

        alert("创建成功");
        document.getElementById("questionJ-create-question").value = "";
        document.getElementById("questionJ-create-analysis").value = "";
        $("#createJudgeQuestionModal").modal('hide');
    }
}

function renderQuestionList() {
    let tmp = '' +
        '<div class="width-full margin-b1 flex flex-row justify-start align-center" style="height: 3.2rem">' +
        '<div class="margin-r1 font-weight-bolder" style="width: 24rem; white-space: nowrap">{0}</div>' +
        '<div class="margin-r1" style="width: 6rem; white-space: nowrap">{1}</div>' +
        '<div class="margin-r1" style="width: 32rem; overflow: auto; white-space: nowrap">{2}</div>' +
        '<div class="flex-empty"></div>' +
        '<button name="questionBtn" data-toggle="modal" data-target="#deleteQuestionModal" ' +
        'type="button" class="btn btn-danger margin-r1" ' +
        'value="{3}" onclick="window.deleteQuestion=this.value">删除题目</button>' +
        '</div>';

    let response = window.Action_question_List(window.question_selectBankUUID);
    if (response === null || response === undefined) {
        alert("网络错误");
        return;
    }
    if (response.Status.Code !== "Suc") {
        alert(response.Status.Message);
        return;
    }
    let questions = response.Questions;

    let listInnerHtml = "";
    for (let i = 0; i < questions.length; i++) {
        let uuid = questions[i].UUID;
        let type = questions[i].Type;
        let QuestionBase64 = questions[i].QuestionBase64;

        let t = "";
        let q = "";
        if (type === "Single") {
            t = "单选题"
        } else if (type === "Multiple") {
            t = "多选题"
        } else {
            t = "判断题"
        }
        q = tools.UnBase64(QuestionBase64);

        listInnerHtml += tools.StrFormat(tmp, [uuid, t, q, uuid]);
    }

    document.getElementById("questionList").innerHTML = listInnerHtml;
}

function deleteQuestion() {
    document.getElementById("question-delete-btn-submit").onclick = function () {
        let questionUUID = window.deleteQuestion;
        let inputQuestion = document.getElementById("question-delete-uuid").value;
        if (questionUUID !== inputQuestion) {
            alert("请确认题目");
            return;
        }

        let response = window.Action_question_Delete(questionUUID);
        if (response === null || response === undefined) {
            alert("网络错误");
            return;
        }
        if (response.Status.Code !== "Suc") {
            alert(response.Status.Message);
            return;
        }

        alert("删除成功");
        document.getElementById("question-delete-uuid").value = "";
        $("#deleteQuestionModal").modal('hide');
    }
}
